home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Frameworks / MADE 1.0.1 / Essential Menus.c < prev    next >
Encoding:
Text File  |  1997-06-04  |  4.7 KB  |  208 lines  |  [TEXT/CWIE]

  1. // MADE - Macintosh Application Development Essentials
  2. // ---------------------------------------------------
  3.  
  4. // (c) Gideon Greenspan, Sig Software - June 1997 - http://www.kagi.com/gdg/
  5.  
  6. // These files can only be used for experimental standalone purposes. To obtain
  7. // fully commented code, and licenses for standalone, shareware, internal and
  8. // commercial usage, run the enclosed Register application.
  9.  
  10. // Essential Menus.h
  11. //
  12. // Menu bar handling, item enabling, Drag Manager and cursor routines.
  13. //
  14. // Version 1.0.0 - 10th November 1996
  15. // Version 1.0.1 - 4th June 1997
  16. //                 Fixed resetting port in DragTrackHandler
  17.  
  18. #include "Essential Headers.h"
  19. #include "Essential Prototypes.h"
  20.  
  21. #if Use_Drag_Manager
  22.  
  23. // NOTE : UPP (Universal Procedure Pointers) are used to ensure compatiblity under 680x0,
  24. // PowerPC, and who knows what other future compilers.
  25.  
  26.     #include <Drag.h>
  27.     
  28. // No support is provided here for data send procedures, but these can be added if needed
  29.     
  30.     pascal OSErr            DragTrackHandler(DragTrackingMessage, WindowPtr, void *, DragReference);
  31.     pascal OSErr            DragReceivHandler(WindowPtr, void*, DragReference);
  32.     DragTrackingHandlerUPP    DragTrackUPP;
  33.     DragReceiveHandlerUPP    DragReceivUPP;
  34.  
  35. #endif
  36.  
  37. // Menu item switching is based on the fact that the upper 16 bits of the value returned by
  38. // the system are the menu's ID and the lower 16 bits are the item number chosen in that menu.
  39. // Just remember these are IDs defined within the MENU resource, not the resource ID itself.
  40.  
  41. enum {
  42.     AppleStart=128*65536,
  43.     AppleAbout,
  44.     AppleSep1,
  45.     AppleItems,
  46.     AppleEnd=128*65536+65535
  47. };
  48.  
  49. Error InitMenuBar()
  50. {
  51.     Error        error=0;
  52.     Handle        theMenuBar;
  53.     MenuHandle    theAppleMenu;
  54.     
  55.     theMenuBar=GetNewMBar(128);
  56.     error=TestResError(theMenuBar);
  57.         _i(error)
  58.     
  59.     SetMenuBar(theMenuBar);
  60.     
  61.     theAppleMenu=GetMHandle(128);
  62.     AddResMenu(theAppleMenu, 'DRVR');
  63.     
  64.     DrawMenuBar();
  65.     
  66.     _e
  67.     return error;
  68. }
  69.  
  70. void SelectMenuItem(long selection)
  71. {
  72.     Str255    menuItemName;
  73.     
  74.     if (selection>=AppleItems && selection<=AppleEnd) {
  75.         GetItem(GetMHandle(128), selection-AppleStart, menuItemName);
  76.         OpenDeskAcc(menuItemName);
  77.     } else
  78.         MyPerformMenu(selection);
  79.     
  80.     HiliteMenu(0);
  81. }
  82.  
  83. void EnableMenuItem(short menuID, short menuItem, Boolean enable)
  84. {
  85.     MenuHandle    menuHandle;
  86.     
  87.     menuHandle=GetMHandle(menuID);
  88.     
  89.     if (enable) EnableItem(menuHandle, menuItem);
  90.     else DisableItem(menuHandle, menuItem);
  91. }
  92.  
  93. void ShowResCursor(short cursorID)
  94. {
  95.     CursPtr    theCursor;
  96.     
  97.     theCursor=*GetCursor(cursorID);
  98.     SetCursor(theCursor);
  99. }
  100.  
  101. #if Use_Drag_Manager
  102.     
  103.     // In this model, a single Track and Receive handler is used for all drags. You can make
  104.     // separate ones for each window. Also, the refCon parameter is left at zero here.
  105.  
  106.     Error InitDragManager()
  107.     {
  108.         Error    error=0;
  109.         
  110.         DragTrackUPP=NewDragTrackingHandlerProc(DragTrackHandler);
  111.         error=InstallTrackingHandler(DragTrackUPP, 0, 0);
  112.         TestError(error);
  113.             _i(error);
  114.         
  115.         DragReceivUPP=NewDragReceiveHandlerProc(DragReceivHandler);
  116.         error=InstallReceiveHandler(DragReceivUPP, 0, 0);
  117.         TestError(error);
  118.         
  119.         _e
  120.         return error;
  121.     }
  122.  
  123.     pascal OSErr DragTrackHandler(DragTrackingMessage message, WindowPtr inWindow,
  124.         void* refCon, DragReference dragRef)
  125.     {
  126.         Point            localPoint;
  127.         DragAttributes    attributes;
  128.         RgnHandle        region;
  129.         GrafPtr            oldPort;
  130.         Error            error=0;
  131.         
  132.         error=GetDragAttributes(dragRef, &attributes);
  133.             _i(error)
  134.         GetPort(&oldPort);
  135.  
  136.         switch (message) {
  137.  
  138.             case dragTrackingInWindow:
  139.                 if (attributes&dragHasLeftSenderWindow) {
  140.  
  141.                     error=GetDragMouse(dragRef, &localPoint, 0);
  142.                         _i(error)
  143.                     
  144.                     SetPort(inWindow);
  145.                     GlobalToLocal(&localPoint);
  146.                     
  147.                     region=MyGetDragRegion(inWindow, localPoint, dragRef);
  148.                     
  149.                     if (region) {
  150.                         ShowDragHilite(dragRef, region, true);
  151.                         DisposeRgn(region);
  152.                     
  153.                     } else
  154.                         HideDragHilite(dragRef);
  155.                     
  156.                 }
  157.                 break;
  158.                 
  159.              case dragTrackingLeaveWindow: case dragTrackingLeaveHandler:
  160.                 SetPort(inWindow);
  161.                 HideDragHilite(dragRef);
  162.                 break;
  163.  
  164.         }
  165.         SetPort(oldPort);
  166.         
  167.         _e
  168.         return error;
  169.     }
  170.  
  171.     pascal OSErr DragReceivHandler(WindowPtr inWindow, void* refCon, DragReference dragRef)
  172.     {
  173.         Error        error=0;
  174.         WindowPtr    oldPort;
  175.         Point        localPoint;
  176.         
  177.         error=GetDragMouse(dragRef, &localPoint, 0);
  178.             _i(error)
  179.             
  180.         GetPort(&oldPort);
  181.         SetPort(inWindow);
  182.         GlobalToLocal(&localPoint);
  183.         SetPort(oldPort);
  184.         
  185.         MyReceiveDrag(inWindow, localPoint, dragRef);
  186.         
  187.         _e
  188.         return error;
  189.     }
  190.  
  191.     void CreateDragRegion(RgnHandle region)
  192.     {
  193.         RgnHandle    subtractRgn;
  194.         Point        dummyPoint;
  195.         
  196.         subtractRgn=NewRgn();
  197.         CopyRgn(region, subtractRgn);
  198.         InsetRgn(subtractRgn, 1, 1);
  199.         DiffRgn(region, subtractRgn, region);
  200.         DisposeRgn(subtractRgn);
  201.         
  202.         dummyPoint.h=dummyPoint.v=0;
  203.         LocalToGlobal(&dummyPoint);
  204.         OffsetRgn(region, dummyPoint.h, dummyPoint.v);
  205.     }
  206.         
  207. #endif
  208.